<?php

namespace app\task\cron\coupon;

use app\common\service\order\CouponSyncOrderService;
use yunwuxin\cron\Task;
use api\channel\jutuike\Coupon;
use think\facade\Db;

/**
 * 同步聚推客会员卡券CPS订单.
 *
 * 同步频率：1分钟执行1次，后期根据实际运行情况做调整
 */
class SyncJutuikeOrder extends Task
{
    /**
     * 配置指令.
     */
    protected function configure()
    {
        // 分 时 日 月 周
        $this->expression = '*/2 * * * *';
    }

    protected function handle()
    {
        echo '同步聚推客会员卡券CPS订单-开始' . PHP_EOL;
        $lockKey = 'coupon_sync.jutuike.order_lock';
        $lockVal = cache($lockKey);
        if ($lockVal) {
            echo '还在处理呢' . PHP_EOL;
            if (!in_array(env('APP_ENV'),['test','local'])){
                return true;
            }
        }
        cache($lockKey, 1, 300);
        $service = new CouponSyncOrderService();
        $service->syncAllOrder();

        cache($lockKey, null);

        echo '同步聚推客会员卡券CPS订单-结束'.PHP_EOL;
    }

}
